package persistent.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TCloudRole;
import model.TCloudRoleSystemModel;
import model.TCloudSystemModel;
import model.TCloudUser;
import model.TSchool;
import persistent.dao.SchoolDAO;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.LogUtil;
import util.toolkit.TimeUtil;

public class SchoolImpl implements SchoolDAO {
	
	private HibBaseDAO dao;
	private static final Log log = LogFactory.getLog(ParentInfoImpl.class);
	
	public SchoolImpl() {
		dao=new HibBaseDAO();
	}

	@Override
	public boolean addSchool(TSchool school, TCloudUser user) {
		Session session1 = HibernateSessionFactory.getSession();
		Transaction tx1 = null;
		tx1 = session1.beginTransaction();
		TimeUtil newtime=new TimeUtil();
		
		String hql = "from TCloudSystemModel";
		List<TCloudSystemModel> menulist = dao.select(hql);
		
		try {			
			session1.save(school);
			int adminroleid = 0;
			for (int i = 0; i < 6; i++) {				
				switch (i) {
				case 0:
					TCloudRole adminrole = new TCloudRole();
					adminrole.setDeepth(1);
					adminrole.setDescription("管理系统");
					adminrole.setName("超级管理员");
					adminrole.setParentid(Constant.SUPERADMIN);
					adminrole.setSchoolid(school.getSchoolid());
					adminrole.setStatus("启用");
					adminrole.setIsedit(false);
					Serializable key = session1.save(adminrole); //先保存角色信息
					int row = ((Integer) key).intValue(); //得到自动编号
					adminroleid = row;
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(true);
						crsm.setRoleid(row);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}				
					break;
				case 1:
					TCloudRole parentrole = new TCloudRole();
					parentrole.setDeepth(1);
					parentrole.setDescription("呵护孩子");
					parentrole.setName("家长");
					parentrole.setParentid(Constant.PARENT);
					parentrole.setSchoolid(school.getSchoolid());
					parentrole.setStatus("启用");
					parentrole.setIsedit(false);
					Serializable key1 = session1.save(parentrole); //先保存角色信息
					int row1 = ((Integer) key1).intValue(); //得到自动编号
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(false);
						crsm.setRoleid(row1);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}	
					break;
				case 2:
					TCloudRole psyTearole = new TCloudRole();
					psyTearole.setDeepth(1);
					psyTearole.setDescription("心理咨询");
					psyTearole.setName("心理健康教师");
					psyTearole.setParentid(Constant.TEACHER);
					psyTearole.setSchoolid(school.getSchoolid());
					psyTearole.setStatus("启用");
					psyTearole.setIsedit(false);
					Serializable key2 = session1.save(psyTearole); //先保存角色信息
					int row2 = ((Integer) key2).intValue(); //得到自动编号
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(false);
						crsm.setRoleid(row2);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}	
					break;
				case 3:
					TCloudRole principalrole = new TCloudRole();
					principalrole.setDeepth(1);
					principalrole.setDescription("学校管理");
					principalrole.setName("校长");
					principalrole.setParentid(Constant.TEACHER);
					principalrole.setSchoolid(school.getSchoolid());
					principalrole.setStatus("启用");
					principalrole.setIsedit(true);
					Serializable key3 = session1.save(principalrole); //先保存角色信息
					int row3 = ((Integer) key3).intValue(); //得到自动编号
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(false);
						crsm.setRoleid(row3);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}	
					break;
				case 4:
					TCloudRole headmasterrole = new TCloudRole();
					headmasterrole.setDeepth(1);
					headmasterrole.setDescription("班级管理");
					headmasterrole.setName("班主任");
					headmasterrole.setParentid(Constant.TEACHER);
					headmasterrole.setSchoolid(school.getSchoolid());
					headmasterrole.setStatus("启用");
					headmasterrole.setIsedit(true);
					Serializable key4 = session1.save(headmasterrole); //先保存角色信息
					int row4 = ((Integer) key4).intValue(); //得到自动编号
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(false);
						crsm.setRoleid(row4);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}
					break;
				case 5:
					TCloudRole subtearole = new TCloudRole();
					subtearole.setDeepth(1);
					subtearole.setDescription("教授知识");
					subtearole.setName("专任教师");
					subtearole.setParentid(Constant.TEACHER);
					subtearole.setSchoolid(school.getSchoolid());
					subtearole.setStatus("启用");
					subtearole.setIsedit(true);
					Serializable key5 = session1.save(subtearole); //先保存角色信息
					int row5 = ((Integer) key5).intValue(); //得到自动编号
					for (int j = 0; j < menulist.size(); j++) {
						TCloudRoleSystemModel crsm = new TCloudRoleSystemModel();
						crsm.setIsedit(false);
						crsm.setRoleid(row5);
						crsm.setCreatedate(newtime.toTimestamp(new Date()).toString());
						crsm.setSysid(menulist.get(j).getId());
						session1.save(crsm);
					}
					break;
				}
			}
			user.setRoleid(adminroleid);
			session1.save(user);
			tx1.commit();
			session1.close();//关闭连接
			return true;
		} catch (Exception e) {
			if(tx1 != null) tx1.rollback();
			session1.close(); //关闭连接
			log.error("persistent.impl包SchoolImpl类addSchool(TSchool school, TCloudUser user)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("persistent.impl包SchoolImpl类addSchool(TSchool school, TCloudUser user)方法调用错误：");
			e.printStackTrace();
		}
		return false;		
	}

	@Override
	public boolean delSchool(String schoolid) {
		return dao.delete(TSchool.class, schoolid);
	}

	@Override
	public boolean updateSchool(TSchool school) {
		return dao.update(school);
	}

	@Override
	public TSchool getTSchool(String schoolid) {
		return (TSchool)dao.findById(TSchool.class, schoolid);
	}

	@Override
	public List<TSchool> getSchoolByCondition(String wherecondition) {
		String hql = "from TSchool " + wherecondition;
		return dao.select(hql);
	}

	@Override
	public List<TSchool> getSchoolByCondition(String wherecondition,
			int currentPage, int pageSize) {
		String hql = "from TSchool " + wherecondition;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

}
